package com.jrmf.redenvelopes.order.job;

import com.jrmf.redenvelopes.order.common.ApplicationContextProvider;
import com.jrmf.redenvelopes.order.dao.AccountCheckResultMapper;
import com.jrmf.redenvelopes.order.dao.JdpaySignPayMapper;
import com.jrmf.redenvelopes.order.dbRouter.DynamicDataSourceContextHolder;
import com.jrmf.redenvelopes.order.model.JdpaySignPay;
import org.quartz.Job;
import org.quartz.JobDetail;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class AccountCheckJob implements Job {
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        //获取依赖 AccountCheckResultMapper
        JdpaySignPayMapper jdpaySignPayMapper = ApplicationContextProvider.getBean("jdpaySignPayMapper");
        AccountCheckResultMapper accountCheckResultMapper = ApplicationContextProvider.getBean("accountCheckResultMapper");

        //获取任务参数
        JobDetail detail = jobExecutionContext.getJobDetail();
        String contrastDate = detail.getJobDataMap().getString("contrastDate");
        String platformType = detail.getJobDataMap().getString("platformType");
        if (null!= contrastDate && !"".equals(contrastDate) && null!= platformType && !"".equals(platformType)){
            //执行对账
            Map<String ,Object> param =new HashMap<>();

            param.put("dateTime",contrastDate);
            param.put("thirdpartyPaytype",platformType);
            List<Map<String,Object>> list1 =jdpaySignPayMapper.accountThird2Mf2(param);//短款
            System.out.println(list1.size());
            List<Map<String,Object>> list2 =jdpaySignPayMapper.accountMf2Third2(param);//长款
            System.out.println(list2.size());
            List<Map<String,Object>> list3 =jdpaySignPayMapper.accountThirdwithMf2(param);//双方都有 但是本地不是成功状态
            System.out.println(list3.size());
            List<Map<String,Object>> finalList =new ArrayList<>();
            if (list1.size()>0){
                finalList.addAll(list1);
            }
            if (list2.size()>0){
                finalList.addAll(list2);
            }
            if (list3.size()>0){
                finalList.addAll(list3);
            }
            int sourceCount =finalList.size();


            if (sourceCount>0){ //有待插入数据
                if (sourceCount>1024){
                    //分批次插入
                    int pageSize =0;
                    if(sourceCount%1024==0){
                        pageSize=sourceCount/2014;
                    }
                    else{
                        pageSize=(sourceCount/1024)+1;
                    }
                    int subSuccessCount =0;
                    for(int i=1;i<=pageSize;i++){
                        //创建新的
                        if(i==pageSize){
                            //从 (i-1)*1024--totalNum
                            List<Map<String,Object>> subList= finalList.subList((i-1)*1024,sourceCount);
                            //System.out.println(subList);
                            int r =accountCheckResultMapper.batchInsert(subList);
                            if (r==(sourceCount-(i-1)*1024)){
                                subSuccessCount=subSuccessCount+r;
                            }
                            else {
                                break;
                            }

                        }
                        else{
                            //从(i-1)*1024--i*1024
                            List<Map<String,Object>> subList= finalList.subList((i-1)*1024,1024*(i));
                            int r=accountCheckResultMapper.batchInsert(subList);
                            if (r==1024){
                                subSuccessCount =subSuccessCount+r;
                            }
                            else{
                                break;
                            }
                        }
                    }
                    if (subSuccessCount ==sourceCount){
                        logger.info("对账差错数据持久化成功！");
                    }
                    else{
                        logger.info("对账差错数据持久化失败！");
                    }
                }
                else{
                    //单批次直接插入
                    int result =accountCheckResultMapper.batchInsert(finalList);
                    if (result ==sourceCount){//判断是否插入无误
                        logger.info("对账差错数据持久化成功！");
                    }
                    else{
                        logger.info("对账差错数据持久化失败！");
                    }
                }
            }

            else {
                logger.info(contrastDate+"全部对齐");
            }

           /* if (fCount>0){
                int result =accountCheckResultMapper.batchInsert(finalList);
                if (result==finalList.size()){
                    logger.info("对账差错数据持久化成功！");
                }
            }*/


        }
        //持久化差错结果和对账log
        // 全部对齐 / 每种差错类型各自多少条 todo

    }
}
